﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.DigitalTwinAlignment.CApDataManipulator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExportToPLMX.Managers;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.IO.Compression;
using System.Runtime.CompilerServices;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.DigitalTwinAlignment;

public static class CApDataManipulator
{
  private const string AppRegistryPath = "HKEY_CLASSES_ROOT\\SRAPP";
  private const string DefaultAppPath = "C:\\PSAR\\PSAR.exe";
  private const string WeldsJSON = "welds.json";

  internal static void ExportModel(string exportPath = "C:\\Temp\\study_internal.plmx")
  {
    CApExportToPlmxFileManager toPlmxFileManager = new CApExportToPlmxFileManager(exportPath);
    if (!toPlmxFileManager.CreateTempFolder())
      return;
    try
    {
      CApExportToPlmxSceneManager plmxSceneManager = new CApExportToPlmxSceneManager(toPlmxFileManager.SceneExportPath);
      plmxSceneManager.ExportScene(toPlmxFileManager.SimulationExportPath, 0);
      CApExportToPlmxPackageManager plmxPackageManager = new CApExportToPlmxPackageManager(toPlmxFileManager.TempRootExportFolder, "Demo", plmxSceneManager.ExportedObjectsCount);
      plmxPackageManager.CreatStudyPreviewImage();
      plmxPackageManager.CreatePackageInfo();
      new CApExportToPlmxHierarchyManager(toPlmxFileManager.TempRootExportFolder).ExportHierarchy(plmxSceneManager.ExportedObjectsIds);
      toPlmxFileManager.CreatePlmx();
      using (FileStream fileStream = new FileStream(exportPath, FileMode.Open))
      {
        using (ZipArchive zipArchive = new ZipArchive((Stream) fileStream, ZipArchiveMode.Update))
        {
          using (StreamWriter streamWriter = new StreamWriter(zipArchive.CreateEntry("welds.json").Open()))
          {
            string weldLocationsJson = CApDataManipulator.GetWeldLocationsJson();
            streamWriter.Write(weldLocationsJson);
          }
        }
      }
    }
    catch (Exception ex)
    {
      TxApplication.LogWriter.WriteExceptionLine(ex);
    }
  }

  private static string GetWeldLocationsJson()
  {
    string str1 = "[ {\"name\":\"none\", \"position\":{\"x\":0,\"y\":0,\"z\":0} }";
    foreach (TxWeldLocationOperation allDescendant in (Collection<ITxObject>) TxApplication.ActiveDocument.OperationRoot.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxWeldLocationOperation))))
    {
      TxVector translation = allDescendant.AbsoluteLocation.Translation;
      string str2 = FormattableString.Invariant(FormattableStringFactory.Create("{0:0.######}", (object) translation.X));
      string str3 = FormattableString.Invariant(FormattableStringFactory.Create("{0:0.######}", (object) translation.Y));
      string str4 = FormattableString.Invariant(FormattableStringFactory.Create("{0:0.######}", (object) translation.Z));
      string str5 = FormattableString.Invariant(FormattableStringFactory.Create("{0:0.########}", (object) allDescendant.AbsoluteLocation[0, 2]));
      string str6 = FormattableString.Invariant(FormattableStringFactory.Create("{0:0.########}", (object) allDescendant.AbsoluteLocation[1, 2]));
      string str7 = FormattableString.Invariant(FormattableStringFactory.Create("{0:0.########}", (object) allDescendant.AbsoluteLocation[2, 2]));
      str1 = $"{str1}, {{ \"name\":\"{allDescendant.Name}\",\"position\": {{\"x\":{str2},\"y\":{str3},\"z\":{str4}}} ,\"normal\": {{\"x\":{str5},\"y\":{str6},\"z\":{str7}}} }}";
    }
    return str1 + "]";
  }

  internal static TxTransformation TxTransformationFromRotationAndTranslation(string[] alignData)
  {
    return new TxTransformation(new TxVector(double.Parse(alignData[3]), double.Parse(alignData[4]), double.Parse(alignData[5])), new TxVector(double.Parse(alignData[6]), double.Parse(alignData[7]), double.Parse(alignData[8])), (TxTransformation.TxRotationType) 1);
  }

  internal static TxTransformation TxTransformationFromValues(string[] alignData)
  {
    double[] numArray = new double[12];
    for (int index = 0; index < 12; ++index)
      numArray[index] = double.Parse(alignData[index + 3]);
    return new TxTransformation(numArray[0], numArray[1], numArray[2], numArray[3], numArray[4], numArray[5], numArray[6], numArray[7], numArray[8], numArray[9], numArray[10], numArray[11]);
  }

  internal static void Align(
    string id,
    string name,
    TxTransformation transformation,
    Func<TxTransformation, TxTransformation, TxTransformation> transformationFunc)
  {
    foreach (ITxLocatableObject allDescendant in (Collection<ITxObject>) TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxLocatableObject))))
    {
      if (id.EndsWith(((ITxObject) allDescendant).Id) && name == ((ITxObject) allDescendant).Name)
        allDescendant.AbsoluteLocation = transformationFunc(allDescendant.AbsoluteLocation, transformation);
    }
  }
}
